package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjRoleAuthor;
import com.zb.mapper.ZjRoleAuthorMapper;
import com.zb.service.ZjRoleAuthorService;
import com.zb.vo.AuthorVo;
import com.zb.vo.MenuAuthorMetaVo;
import com.zb.vo.MenuAuthorVo;
import com.zb.vo.RoleAuthorVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Service
public class ZjRoleAuthorServiceImpl extends ServiceImpl<ZjRoleAuthorMapper, ZjRoleAuthor> implements ZjRoleAuthorService {
    @Resource
    private ZjRoleAuthorMapper roleAuthorMapper;

    // 保存或修改角色权限
    @Override
    @Transactional
    public void save(Long custAccId, Long roleId, List<RoleAuthorVo> roleAuthorVos) {
        roleAuthorMapper.deleteByRoleId(roleId);
        if (CollectionUtil.isEmpty(roleAuthorVos)) {
            return;
        }
        List<ZjRoleAuthor> roleAuthors = new ArrayList<>();
        ZjRoleAuthor roleAuthor = null;
        for (RoleAuthorVo vo : roleAuthorVos) {
            roleAuthor = new ZjRoleAuthor();
            roleAuthor.setId(IdWorker.getId());
            roleAuthor.setCustAccId(custAccId);
            roleAuthor.setRoleId(roleId);
            roleAuthor.setAuthorId(vo.getAuthorId());
            roleAuthor.setParentAuthorId(0L);
            roleAuthors.add(roleAuthor);
            getRoleAuthorData(custAccId, roleId, vo.getAuthorId(), roleAuthors, vo.getAuthorVos());
        }
        this.saveBatch(roleAuthors);
    }

    private void getRoleAuthorData(Long custAccId, Long roleId, Long parentAuthorId, List<ZjRoleAuthor> roleAuthors, List<RoleAuthorVo> roleAuthorVos) {
        if (CollectionUtil.isEmpty(roleAuthorVos)) {
            return;
        }
        ZjRoleAuthor roleAuthor = null;
        for (RoleAuthorVo vo : roleAuthorVos) {
            roleAuthor = new ZjRoleAuthor();
            roleAuthor.setId(IdWorker.getId());
            roleAuthor.setCustAccId(custAccId);
            roleAuthor.setRoleId(roleId);
            roleAuthor.setAuthorId(vo.getAuthorId());
            roleAuthor.setParentAuthorId(parentAuthorId);
            roleAuthors.add(roleAuthor);
            getRoleAuthorData(custAccId, roleId, vo.getAuthorId(), roleAuthors, vo.getAuthorVos());
        }
    }

    // 根据商家ID获取系统超管权限
    @Override
    public List<ZjRoleAuthor> getAdminAuthorByCustAccId(Long custAccId) {
        return roleAuthorMapper.getAdminAuthorByCustAccId(custAccId);
    }

    // 根据角色ID获取权限
    @Override
    public List<ZjRoleAuthor> getRoleAuthorByRoleId(Long roleId) {
        QueryWrapper<ZjRoleAuthor> wrapper = new QueryWrapper<>();
        wrapper.select("author_id", "parent_author_id")
                .eq("role_id", roleId);
        return this.list(wrapper);
    }

    // 根据商家ID & 角色ID 获取菜单权限
    @Override
    public List<AuthorVo> getAuthorByCustAccIdAndRoleId(Long custAccId, Long roleId) {
        return roleAuthorMapper.getAuthorByCustAccIdAndRoleId(custAccId, roleId);
    }

}
